#include<iostream>
#include<vector>
#include<pthread.h>
const int num = 5;
void* handler(void* args)
{
    char* threadname = static_cast<char*>(args);
    std::string th_name = std::string(threadname);
    std::cout<<th_name<<std::endl;
    return nullptr;
}
class Task
{
    private:
        int _x;
        int _y;
    public:
        Task(int x,int y):_x(x),_y(y){}

        int add(){return _x + _y;}
};
class ThreadTask
{
private:
    std::string _threadname;
    Task task;
public:
    ThreadTask(int a,int b,std::string threadname):task(a,b),_threadname(threadname){
    }
    int Run(){
        return task.add();
    }
    void Print(){
        std::cout<<_threadname<<std::endl;
    }
};
class Result
{
    private:
        int _result;
    public:
        Result(int result):_result(result){
        }
        void Print(){
            std::cout<<_result<<std::endl;
        }
};
void* handlerTask(void* args)
{
    ThreadTask* threadtask = static_cast<ThreadTask*>(args);
    
    Result* res = new Result(threadtask->Run());
    return res;
}
int main()
{
    std::vector<pthread_t> threads;
    // 创建线程
    for(int i = 0;i<num;i++)
    {
        char* threadname = new char[64];
        snprintf(threadname,64,"thread--%d",i+1);
        pthread_t tid;
        ThreadTask* tasks = new ThreadTask(10,24,threadname);
        pthread_create(&tid,nullptr,handlerTask,tasks);
        // 保存起来
        threads.push_back(tid);
    }
    void* res;
    std::vector<Result*> results;
    for(auto it:threads)
    {
        pthread_join(it,&res);
        results.push_back((Result*)res);

    }

    for(auto res: results){
        res->Print();
        std::cout<<"afdfadfdas"<<std::endl;
    }

    return 0;
}